<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-row>
              <el-col :span="6">
                <el-form-item label="下单时间" prop="payTime">
                  <el-date-picker
                    v-model="dateRange"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    type="datetimerange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <!--          <el-col :span="1.5">-->
          <!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['lawyer:payOrder:add']">新增</el-button>-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['lawyer:payOrder:edit']">修改</el-button>-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['lawyer:payOrder:remove']">删除</el-button>-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['lawyer:payOrder:export']">导出</el-button>-->
          <!--          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="spuOrderStatList" @selection-change="handleSelectionChange">
        <el-table-column label="主键ID" align="center" prop="index" v-if="false" />
        <el-table-column label="订单总数 / 销售额" align="center" prop="orderCount">
          <template #default="scope">
            <el-text>{{ scope.row.orderCount }} / {{ scope.row.orderAmount / 100 }} 元</el-text>
          </template>
        </el-table-column>
        <el-table-column label="7天(订单总数 / 销售额)" align="center" prop="orderCount">
          <template #default="scope">
            <el-text>{{ scope.row.orderCount }} / {{ scope.row.orderAmount / 100 }} 元</el-text>
          </template>
        </el-table-column>
        <el-table-column label="30天(订单总数 / 销售额)" align="center" prop="orderCount">
          <template #default="scope">
            <el-text>{{ scope.row.orderCount }} / {{ scope.row.orderAmount / 100 }} 元</el-text>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total < 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList"  />
    </el-card>
    <!-- 添加或修改支付订单对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="payOrderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="支付应用ID" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入支付应用ID" />
        </el-form-item>
        <el-form-item label="微信appid" prop="wxAppId">
          <el-input v-model="form.wxAppId" placeholder="请输入微信appid" />
        </el-form-item>
        <el-form-item label="商户号" prop="mchNo">
          <el-input v-model="form.mchNo" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="商户名称" prop="mchName">
          <el-input v-model="form.mchName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="通道ID" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入通道ID" />
        </el-form-item>
        <el-form-item label="通道编码" prop="channelCode">
          <el-input v-model="form.channelCode" placeholder="请输入通道编码" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="用户openId" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入用户openId" />
        </el-form-item>
        <el-form-item label="支付单号" prop="payNo">
          <el-input v-model="form.payNo" placeholder="请输入支付单号" />
        </el-form-item>
        <el-form-item label="第三方交易号" prop="tradeNo">
          <el-input v-model="form.tradeNo" placeholder="请输入第三方交易号" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="商品标题" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入商品标题" />
        </el-form-item>
        <el-form-item label="商品描述" prop="body">
          <el-input v-model="form.body" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="付款方式" prop="payWay">
          <el-input v-model="form.payWay" placeholder="请输入付款方式" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
                          v-model="form.payTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="步骤(1 创建支付，2 支付回调通知)" prop="payStep">
          <el-input v-model="form.payStep" placeholder="请输入步骤(1 创建支付，2 支付回调通知)" />
        </el-form-item>
        <el-form-item label="支付金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model="form.refundPrice" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="第三方返回的错误码" prop="resultCode">
          <el-input v-model="form.resultCode" placeholder="请输入第三方返回的错误码" />
        </el-form-item>
        <el-form-item label="第三方返回的错误信息" prop="resultMsg">
          <el-input v-model="form.resultMsg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="提交的信息" prop="postJson">
          <el-input v-model="form.postJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="回调的信息" prop="returnJson">
          <el-input v-model="form.returnJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="回调时间" prop="returnTime">
          <el-date-picker clearable
                          v-model="form.returnTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择回调时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回调地址" prop="notifyUrl">
          <el-input v-model="form.notifyUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="手续费比率" prop="procedureKb">
          <el-input v-model="form.procedureKb" placeholder="请输入手续费比率" />
        </el-form-item>
        <el-form-item label="手续费" prop="procedureFee">
          <el-input v-model="form.procedureFee" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="订单退款成功时间" prop="successTime">
          <el-date-picker clearable
                          v-model="form.successTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择订单退款成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="扩展参数" prop="extParam">
          <el-input v-model="form.extParam" placeholder="请输入扩展参数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SalesPerformance" lang="ts">
import { listSpuOrderStat } from '@/api/stat/sales';
import { SpuOrderStatVo, SpuOrderStatForm, SpuOrderStatQuery } from '@/api/stat/sales/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const spuOrderStatList = ref<SpuOrderStatVo[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const spuOrderStatFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SpuOrderStatForm = {
  orderCount: undefined,
  orderAmount: undefined,
  weekOrderCount: undefined,
  weekOrderAmount: undefined,
  monthOrderCount: undefined,
  monthOrderAmount: undefined
};

const data = reactive<PageData<SpuOrderStatForm, SpuOrderStatQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    params: {
    }
  },
  rules: {

  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询统计列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSpuOrderStat(proxy?.addDateRange(queryParams.value, dateRange.value));
  spuOrderStatList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  spuOrderStatFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 导出按钮操作 */
// const handleExport = () => {
//   proxy?.download('lawyer/payOrder/export', {
//     ...queryParams.value
//   }, `PayOrder_${new Date().getTime()}.xlsx`)
// }

onMounted(() => {
  getList();
});
</script>
